package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.EaiSysRoleCategoryDTO;
import org.byqh.dqeai.dto.common.PageData;
import org.byqh.dqeai.entity.EaiSysRoleCategory;
import org.byqh.dqeai.entity.proxy.EaiSysRoleCategoryProxy;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sysrolec")
@Tag(name = "系统[EaiSysRoleCategory] - 角色分类管理")
public class EaiSysRoleCategoryController extends BaseController<EaiSysRoleCategory, EaiSysRoleCategoryProxy> {
    @Override
    @GetMapping(value = "/get/{id}")
    public Json<EaiSysRoleCategory> get(String id) {
        return super.get(id);
    }

    @Override
    @PutMapping(value = "/put")
    public Json<Boolean> put(EaiSysRoleCategory dto) {
        return super.put(dto);
    }

    @Override
    @PostMapping(value = "/post")
    public Json<Boolean> post(EaiSysRoleCategory dto) {
        return super.post(dto);
    }

    @GetMapping(value = "/{id}")
    public Json<EaiSysRoleCategoryDTO> get2(@PathVariable String id) {
        var obj = getEntityQueryable()
                .whereById(id)
                .selectAutoInclude(EaiSysRoleCategoryDTO.class)
                .firstOrNull();

        return Json.success(obj);
    }

    /**
     * 通过appId查找角色分类信息
     */
    @GetMapping(value = "/appId/{id}")
    public Json<List<EaiSysRoleCategoryDTO>> getByAppId(@PathVariable String id) {
        // 角色与应用分离后, 不使用appId
        var list = getEntityQueryable()
                .selectAutoInclude(EaiSysRoleCategoryDTO.class)
                .toList();

        return Json.success(list);
    }

    /**
     * 新增某一个具体对象信息
     */
    @PostMapping(value = "/")
    public Json<Boolean> post(@RequestBody EaiSysRoleCategoryDTO dto) {
        var now = LocalDateTime.now();

        dto.setUpdateDate(now);
        dto.setCreateDate(now);

        return easyEntityQuery.insertable(dto).executeRows() > 0 ? Json.success() : Json.error();
    }

    /**
     * 删除某一个具体对象信息
     */
    @DeleteMapping(value = "/{id}")
    public Json<Boolean> delete(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return Json.error();
        }

        String sql = """
                DELETE from EAI_SYS_ROLE_CATEGORY
                         WHERE CATEGORY_ID IN
                               (select CATEGORY_ID
                                  FROM EAI_SYS_ROLE_CATEGORY
                                 start with CATEGORY_ID = '%s'
                                connect by prior CATEGORY_ID = PARENT_CATEGORY_ID)
                """;

        sql = String.format(sql, id);

        var rows = easyEntityQuery.sqlExecute(sql);

        return rows > 0 ? Json.success() : Json.error();
    }

    /**
     * 修改某一个具体对象信息
     */
    @PutMapping(value = "/")
    public Json<Boolean> put(@RequestBody EaiSysRoleCategoryDTO dto) {
        dto.setUpdateDate(LocalDateTime.now());
        return easyEntityQuery.updatable(dto).executeRows() > 0 ? Json.success() : Json.error();
    }

    /**
     * 查询对象信息分页列表
     */
    @PostMapping(value = "/page")
    public Json<PageData<EaiSysRoleCategoryDTO>> page(@RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer size,
                                                      @RequestParam(defaultValue = "", required = false) String sort,
                                                      @RequestBody(required = false) EaiSysRoleCategoryDTO dto) {

        var pageData = listPageImpl(EaiSysRoleCategoryDTO.class, page, size, null).getData();

        return Json.success(new PageData<>(pageData));
    }

    /**
     * 查询全部对象信息列表
     */
    @GetMapping(value = "/list")
    public Json<List<EaiSysRoleCategoryDTO>> list() {
        return listImpl(EaiSysRoleCategoryDTO.class, query -> {
            return query.orderBy(r -> r.createDate().desc());
        });
    }
}